package com.happyscan138.makefun.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.happyscan138.makefun.BaseAdapter.saokuVideo;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.SaokuVideoAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/20.
 */

public class Saoku_Video_Fragment extends Fragment {
    private Context mContext;
    private ListView listViewSaoku_video;
    private List<saokuVideo> listData;
    private int currentIndex=-1;
    private int playPosition=-1;
    private boolean isPaused=false;
    private boolean isPlaying=false;
    private String SaokuVideo;
    private Intent intent;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.saoku_video, container, false);
        client=new AsyncHttpClient();
        params=new RequestParams();
        initView(view);
        getAdapterView(view);
        return view;
    }
    public void initView(View view) {
        listViewSaoku_video = (ListView) view.findViewById(R.id.saoku_video);
    }
    public View getAdapterView( View view) {

        params.put("page_count","30");
        params.put("last_index_key","0");
        log.e("params",params.toString());
        RequestHandle handle=client.post(Urls.VideoList,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                log.e("jsonStrVideo",jsonStr);
                listData=new ArrayList<>();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject object=jsonArray.getJSONObject(i);
                        saokuVideo data=new saokuVideo();
                        data.video=base+object.getString("sao_resource");
                        listData.add(data);
                    }

                    log.e("listData",listData.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                SaokuVideoAdapter adapter=new SaokuVideoAdapter(getActivity(),listData);
                listViewSaoku_video.setAdapter(adapter);
                adapter.setSaoKuAddClickListener(new SaokuVideoAdapter.SaoKuAddClickListener() {
                    @Override
                    public void onItemClick(String positionText) {
                        SaokuVideo=positionText;
                        intent = new Intent();
                        Bundle  builder=new Bundle();
                        builder.putString("SaokuVideo",SaokuVideo);
                        getActivity().setResult(1,getActivity().getIntent().putExtras(builder));
                        getActivity().finish();
                    }
                });


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("responseString",responseString);
            }
        });
        return view;
    }
}
